const util = require('../../utils/util.js');

Page({
  data: {
    today: '',
    healthPlans: [],
    completedCount: 0,
    totalCount: 0,
    showAddPlanModal: false,
    showCustomPlanForm: false,
    newPlanName: '',
    newPlanTarget: '',
    newPlanUnit: '',
    progress: 0,
    defaultPlans: [
      { name: '每日步行10000步', target: 10000, unit: '步' },
      { name: '每日喝水2000ml', target: 2000, unit: 'ml' },
      { name: '健身30分钟', target: 30, unit: '分钟' },
      { name: '控制饮食热量', target: 0, unit: '千卡' }
    ]
  },

  onLoad: function () {
    // 获取今天日期
    this.setData({
      today: util.formatDate(new Date())
    });
    
    // 加载健康计划
    this.loadHealthPlans();
  },

  onShow: function () {
    // 每次页面显示时重新加载数据
    this.loadHealthPlans();
  },

  // 加载健康计划
  loadHealthPlans: function () {
    const healthPlans = wx.getStorageSync('healthPlans') || [];
    const completedCount = healthPlans.filter(plan => plan.completed).length;
    const progress = healthPlans.length > 0 ? Math.floor(completedCount / healthPlans.length * 100) : 0;
    
    this.setData({
      healthPlans,
      completedCount,
      totalCount: healthPlans.length,
      progress
    });
  },

  // 显示添加计划弹窗
  showAddPlanModal: function () {
    this.setData({
      showAddPlanModal: true,
      showCustomPlanForm: false,
      newPlanName: '',
      newPlanTarget: '',
      newPlanUnit: ''
    });
  },

  // 隐藏添加计划弹窗
  hideAddPlanModal: function () {
    this.setData({
      showAddPlanModal: false
    });
  },

  // 显示自定义计划表单
  showCustomForm: function () {
    this.setData({
      showCustomPlanForm: true
    });
  },

  // 监听输入计划名称变化
  onPlanNameInput: function (e) {
    this.setData({
      newPlanName: e.detail.value
    });
  },

  // 监听输入目标值变化
  onPlanTargetInput: function (e) {
    this.setData({
      newPlanTarget: e.detail.value
    });
  },

  // 监听输入单位变化
  onPlanUnitInput: function (e) {
    this.setData({
      newPlanUnit: e.detail.value
    });
  },

  // 添加默认计划
  addDefaultPlan: function (e) {
    const index = e.currentTarget.dataset.index;
    const plan = this.data.defaultPlans[index];
    
    this.addPlan(plan.name, plan.target, plan.unit);
  },

  // 添加自定义计划
  addCustomPlan: function () {
    const name = this.data.newPlanName.trim();
    const target = parseFloat(this.data.newPlanTarget);
    const unit = this.data.newPlanUnit.trim();
    
    if (!name) {
      wx.showToast({
        title: '请输入计划名称',
        icon: 'none'
      });
      return;
    }
    
    this.addPlan(name, isNaN(target) ? 0 : target, unit);
  },

  // 添加计划通用方法
  addPlan: function (name, target, unit) {
    const healthPlans = wx.getStorageSync('healthPlans') || [];
    
    // 检查是否已有相同名称的计划
    const existingIndex = healthPlans.findIndex(plan => plan.name === name);
    
    if (existingIndex >= 0) {
      wx.showToast({
        title: '已存在相同名称的计划',
        icon: 'none'
      });
      return;
    }
    
    // 添加新计划
    healthPlans.push({
      id: Date.now(), // 使用时间戳作为唯一ID
      name,
      target,
      unit,
      completed: false,
      createdAt: util.formatDate(new Date())
    });
    
    // 保存到本地存储
    wx.setStorageSync('healthPlans', healthPlans);
    
    // 隐藏弹窗并刷新数据
    this.setData({
      showAddPlanModal: false
    });
    
    this.loadHealthPlans();
    
    wx.showToast({
      title: '计划已添加',
      icon: 'success'
    });
  },

  // 切换计划完成状态
  togglePlanStatus: function (e) {
    const id = e.currentTarget.dataset.id;
    const healthPlans = wx.getStorageSync('healthPlans') || [];
    
    // 查找并更新计划完成状态
    const planIndex = healthPlans.findIndex(plan => plan.id === id);
    
    if (planIndex >= 0) {
      healthPlans[planIndex].completed = !healthPlans[planIndex].completed;
      
      // 保存到本地存储
      wx.setStorageSync('healthPlans', healthPlans);
      
      // 刷新数据
      this.loadHealthPlans();
    }
  },

  // 删除计划
  deletePlan: function (e) {
    const id = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '删除计划',
      content: '确定要删除此计划吗？',
      success: res => {
        if (res.confirm) {
          const healthPlans = wx.getStorageSync('healthPlans') || [];
          
          // 过滤掉要删除的计划
          const updatedPlans = healthPlans.filter(plan => plan.id !== id);
          
          // 保存到本地存储
          wx.setStorageSync('healthPlans', updatedPlans);
          
          // 刷新数据
          this.loadHealthPlans();
          
          wx.showToast({
            title: '计划已删除',
            icon: 'success'
          });
        }
      }
    });
  },

  // 重置所有计划完成状态
  resetAllPlans: function () {
    wx.showModal({
      title: '重置计划',
      content: '确定要重置所有计划的完成状态吗？',
      success: res => {
        if (res.confirm) {
          const healthPlans = wx.getStorageSync('healthPlans') || [];
          
          // 将所有计划设置为未完成
          healthPlans.forEach(plan => {
            plan.completed = false;
          });
          
          // 保存到本地存储
          wx.setStorageSync('healthPlans', healthPlans);
          
          // 刷新数据
          this.loadHealthPlans();
          
          wx.showToast({
            title: '计划已重置',
            icon: 'success'
          });
        }
      }
    });
  }
}); 